package cn.edu.zafu.bigdata.lb;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPClockClient {
    // 修改为服务端的IP地址
    private static final String SERVER_IP = "10.201.206.131";
    private static final int SERVER_PORT = 12345;

    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket();
        InetAddress address = InetAddress.getByName(SERVER_IP);

        // 发送请求
        byte[] requestBuffer = "Time please".getBytes();
        DatagramPacket request = new DatagramPacket(requestBuffer, requestBuffer.length, address, SERVER_PORT);
        socket.send(request);

        // 接收响应
        byte[] responseBuffer = new byte[1024];
        DatagramPacket response = new DatagramPacket(responseBuffer, responseBuffer.length);
        socket.receive(response);

        // 打印服务端的时间
        String serverTime = new String(response.getData(), 0, response.getLength());
        System.out.println("Server time is: " + serverTime);
        socket.close();
    }
}